#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "comm.hpp"

int main()
{
    // 利用key值获取共享内存并挂接到程序共享区
    key_t key = GetKey();
    int shmid = GetShm(key);
    char *s = (char *)shmat(shmid, nullptr, 0);
    std::cout << "attach shm done" << std::endl;
    int fd = open(filename.c_str(), O_WRONLY);

    // 向共享内存中写入信息，并利用管道形成同步
    sleep(5);
    for (char c = 'a'; c <= 'z'; c++)
    {
        s[c - 'a'] = c;
        std::cout << "write : " << c << " done" << std::endl;
        sleep(1);
        int code = 1;
        write(fd, &code, sizeof(4));
    }

    // 对共享内存解除挂接，并释放管道
    shmdt(s);
    std::cout << "detach shm done" << std::endl;
    close(fd);
    return 0;
}